﻿using ExcelReport;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel;

namespace ExcelReport
{
    public class WriteToExcel:IDisposable
    {
        public event EventHandler<ConfigToExcelEventArgs> WritingProgress;
        public event EventHandler ReportWrited;

        private Dictionary<string, bool> _visibleSettings = new Dictionary<string, bool>();

        private string _saveAsFileFullPath;

        public bool IsOpen { get; private set; } = false;

        private Excel.Application excelApp;
        private Excel.Workbook workBook;

        public string TemplateFilePath { get; set; }
        public string ReportFilePath { get; set; }
        //另外要保存的地址，如果为空则执行默认的报告模板
        public string SaveAsFileFullPath
        {
            get
            {
                return string.IsNullOrEmpty(_saveAsFileFullPath) ?
                       ReportFilePath :
                       _saveAsFileFullPath;
            }
            set
            {
                _saveAsFileFullPath = value;
            }
        }
        public WriteToExcel() { }

        public void Open()
        {
            if (!File.Exists(TemplateFilePath))
            {
                throw new FileNotFoundException($"模板文件：{TemplateFilePath}不存在！");
            }
            excelApp = new Excel.Application();
            excelApp.DisplayAlerts = false;

            workBook = excelApp.Workbooks.Add(TemplateFilePath);

            IsOpen = true;
        }

        public void Write(List<ExcelContent> reportContents)
        {
            if (IsOpen)
            {
                workBook.Saved = true;
                workBook.SaveAs(TemplateFilePath);

                int count = reportContents.Count;
                int current = 0;
                foreach (ExcelContent report in reportContents)
                {
                    report.WriteTo(workBook);
                    current++;
                    WritingProgressEvent(0, count, current);
                }
                workBook.Saved = true;
                workBook.SaveAs(TemplateFilePath);
                WritedEvent();
            }
        }
        public void Close()
        {
            excelApp?.Workbooks?.Close();
            excelApp?.Quit();
            IsOpen = false;
        }

        private void WritingProgressEvent(int minProgress, int maxProgress, int progress)
        {
            if (WritingProgress != null)
            {
                WritingProgress(this, new ConfigToExcelEventArgs() { MinProgress = minProgress, MaxProgress = maxProgress, Progress = progress });
            }
        }

        private void WritedEvent()
        {
            if (ReportWrited != null)
            {
                ReportWrited(this, new EventArgs());
            }
        }
        private bool disposedValue = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    if (excelApp != null && !excelApp.Visible)
                    {
                        Close();
                    }
                }
                disposedValue = true;
            }
        }
        public void Dispose()
        {
            Dispose(true);
        }


    }
}

